import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Limit } from 'src/shared/utils/pagination';
import { In, Repository } from 'typeorm';
import { User } from './entities/user.entity';
// import { Transactional } from 'src/shared/transaction/decorator/transaction.decorator';
import { TransactionService } from 'src/shared/transaction/transaction.service';
import { CrudService } from 'src/shared/common/crud.service';
import { RoleEntity } from './entities/role.entity';

@Injectable()
export class UserService {
  constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,
    private transactionService: TransactionService,
    private readonly crudService: CrudService<User>,
    @InjectRepository(RoleEntity)
    private readonly roleRepository: Repository<RoleEntity>,
  ) {}

  async list(limit: Limit) {
    const listData = await this.crudService.findAll(limit);
    // 查询角色
    // const roleIds = listData.content.map((item) => item.roleId);
    // const roles = await this.roleRepository.find({
    //   where: {
    //     id: In(roleIds),
    //   },
    // });
    // // 将角色信息添加到用户信息中
    // const userWithRoles = listData.content.map((user) => {
    //   const role = roles.find((role) => role.id === user.roleId);
    //   return {
    //     ...user,
    //     role: role ?? null,
    //   };
    // });
    // 通过用户id
    const userIds = listData.content.map((item) => item.id);
    const roles = await this.roleRepository.find({
      where: {
        userId: In(userIds),
      },
    });
    // return roles;
    // 将角色信息添加到用户信息中
    const userWithRoles = listData.content.map((user) => {
      const role = roles.filter((role) => role.userId === user.id);

      return {
        ...user,
        role: role ?? null,
      };
    });
    return userWithRoles;
  }

  // @Transactional()
  // async createTenantUser(
  //   createUserDto: CreateUserDto,
  //   tenantId: number,
  //   queryRunner?: QueryRunner,
  // ) {
  //   // 创建门店用户 可能是管理员 可能是服务人员
  //   const user = await this.userRepository.findOneBy({
  //     phone: createUserDto.phone,
  //   });
  //   if (user) {
  //     const role = await this.roleRepository.findOneBy({
  //       userId: user.id,
  //       tenantId,
  //     });
  //     if (role) {
  //       throw new HttpException('本店该手机号用户已存在', 400);
  //     }
  //     const newRoleData = queryRunner.manager.create(RoleEntity, {
  //       realname: createUserDto.realname,
  //       userId: user.id,
  //       tenantId,
  //       role: createUserDto.role,
  //     });
  //     const newRole = await queryRunner.manager.save(newRoleData);
  //     return {
  //       user,
  //       role: newRole,
  //     };
  //   }
  //   const newUserData = queryRunner.manager.create(User, {
  //     phone: createUserDto.phone,
  //   });
  //   const newUser = await queryRunner.manager.save(newUserData);
  //   const newRoleData = queryRunner.manager.create(RoleEntity, {
  //     realname: createUserDto.realname,
  //     userId: newUser.id,
  //     tenantId,
  //     role: createUserDto.role,
  //   });
  //   const newRole = await queryRunner.manager.save(newRoleData);
  //   return {
  //     user: newUser,
  //     role: newRole,
  //   };
  // }
}
